<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>会议室预定 - 会议室预订系统</title>
    <!-- 引入Font Awesome图标库 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 重置默认样式并设置全局字体 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        /* 页面背景设置为渐变色，并居中显示容器 */
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        /* 主容器样式：毛玻璃效果、圆角、阴影 */
        .container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 800px;
            padding: 40px;
            position: relative;
            overflow: hidden;
        }

        /* 容器装饰效果：对角线渐变背景 */
        .container::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
            transform: rotate(45deg);
            z-index: 0;
        }

        /* 返回链接容器 */
        .back-link-container {
            margin-bottom: 20px;
            position: relative;
            z-index: 1;
        }

        /* 返回链接样式 */
        .back-link {
            display: inline-flex;
            align-items: center;
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            padding: 8px 16px;
            border-radius: 8px;
        }

        .back-link:hover {
            background: rgba(102, 126, 234, 0.1);
            text-decoration: none;
            transform: translateX(-3px);
        }

        /* 页眉样式设置 */
        .header {
            text-align: center;
            margin-bottom: 30px;
            position: relative;
            z-index: 1;
        }

        /* 标题样式：渐变文字效果 */
        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        /* 表单组样式 */
        .form-group {
            margin-bottom: 25px;
            position: relative;
            z-index: 1;
        }

        /* 表单标签样式 */
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
            font-size: 14px;
        }

        /* 输入框包装器 */
        .input-with-icon {
            position: relative;
        }

        /* 输入框图标样式 */
        .input-with-icon i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #777;
        }

        /* 输入框样式 */
        .form-group input {
            width: 100%;
            padding: 15px 15px 15px 45px;
            border: 2px solid #e1e1e1;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        /* 输入框焦点样式 */
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            background: #fff;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        /* 预订按钮样式 */
        #bookingButton {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            z-index: 1;
        }

        /* 预订按钮悬停效果 */
        #bookingButton:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
        }

        /* 预订按钮按下效果 */
        #bookingButton:active {
            transform: translateY(0);
        }

        /* 页脚样式 */
        .footer {
            text-align: center;
            margin-top: 30px;
            color: #777;
            font-size: 12px;
            position: relative;
            z-index: 1;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .container {
                padding: 30px 20px;
            }

            .header h1 {
                font-size: 24px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="back-link-container">
            <a href="/api/major.html" class="back-link">
                <i class="fas fa-arrow-left"></i> 返回主页
            </a>
        </div>
        <div class="header">
            <h1><i class="fas fa-calendar-plus"></i> 预定会议室</h1>
            <p>请输入会议室预订信息</p>
        </div>

        <!-- 预定表单 -->
        <form id="bookingForm">
            <div class="form-group">
                <label for="roomId">会议室编号</label>
                <div class="input-with-icon">
                    <i class="fas fa-universal-access"></i>
                    <input type="text" id="roomId" name="roomId" placeholder="请输入会议室编号" required>
                </div>
            </div>
            
            <div class="form-group">
                <label for="roomName">会议室名称</label>
                <div class="input-with-icon">
                    <i class="fas fa-building"></i>
                    <input type="text" id="roomName" name="roomName" placeholder="请输入会议室名称" required>
                </div>
            </div>
            
            <div class="form-group">
                <label for="bookerName">预定人姓名</label>
                <div class="input-with-icon">
                    <i class="fas fa-user"></i>
                    <input type="text" id="bookerName" name="bookerName" placeholder="请输入预定人姓名" required>
                </div>
            </div>
            
            <div class="form-group">
                <label for="startTime">会议开始时间</label>
                <div class="input-with-icon">
                    <i class="fas fa-clock"></i>
                    <input type="datetime-local" id="startTime" name="startTime" required>
                </div>
            </div>
            
            <div class="form-group">
                <label for="endTime">会议结束时间</label>
                <div class="input-with-icon">
                    <i class="fas fa-clock"></i>
                    <input type="datetime-local" id="endTime" name="endTime" required>
                </div>
            </div>
            
            <div class="form-group">
                <label for="meetingTopic">会议主题</label>
                <div class="input-with-icon">
                    <i class="fas fa-comment"></i>
                    <input type="text" id="meetingTopic" name="meetingTopic" placeholder="请输入会议主题">
                </div>
            </div>
            
            <button type="submit" id="bookingButton">
                <i class="fas fa-calendar-check"></i> 立即预订
            </button>
        </form>
        
        <div class="footer">
            <p>© 2025 会议室预订系统. 保留所有权利.</p>
        </div>
    </div>

    <script>
        // 页面加载时获取会议室列表
        window.addEventListener('DOMContentLoaded', function() {
            fetch('/api/meetings/rooms')
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        // 存储会议室信息到全局变量
                        window.meetingRooms = data.data;
                        console.log('会议室列表:', window.meetingRooms);
                    } else {
                        console.error('获取会议室列表失败:', data.msg);
                    }
                })
                .catch(error => {
                    console.error('获取会议室列表错误:', error);
                });
        });

        // 为会议室编号输入框添加事件监听器
        document.addEventListener('DOMContentLoaded', function() {
            const roomIdInput = document.getElementById('roomId');
            const roomNameInput = document.getElementById('roomName');
            
            roomIdInput.addEventListener('input', function() {
                const roomId = this.value.trim();
                if (roomId && window.meetingRooms) {
                    const selectedRoom = window.meetingRooms.find(room => room.roomId === roomId);
                    if (selectedRoom) {
                        roomNameInput.value = selectedRoom.roomName;
                    } else {
                        // 如果没有找到对应的会议室，清空会议室名称
                        roomNameInput.value = '';
                    }
                } else {
                    // 如果会议室编号为空，清空会议室名称
                    roomNameInput.value = '';
                }
            });
        });

        // 表单提交事件处理
        document.getElementById('bookingForm').addEventListener('submit', function(e) {
            // 阻止表单默认提交行为
            e.preventDefault();
            
            // 获取表单数据
            const roomId = document.getElementById('roomId').value.trim();
            const bookerName = document.getElementById('bookerName').value.trim();
            let startTime = document.getElementById('startTime').value;
            let endTime = document.getElementById('endTime').value;
            const meetingTopic = document.getElementById('meetingTopic').value;
            
            // 基本验证
            if (!roomId || !bookerName || !startTime || !endTime) {
                alert('请填写所有必填字段');
                return;
            }
            
            // 确保时间格式包含秒并验证格式
            const validateAndFormatTime = (timeStr) => {
                // 如果是 yyyy-MM-ddTHH:mm 格式，补全秒部分
                if (timeStr.length === 16 && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test(timeStr)) {
                    return timeStr + ':00';
                }
                // 如果已经是 yyyy-MM-ddTHH:mm:ss 格式
                else if (timeStr.length === 19 && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/.test(timeStr)) {
                    return timeStr;
                }
                // 格式不正确
                throw new Error('时间格式不正确，请选择正确的日期时间格式');
            };
            
            try {
                startTime = validateAndFormatTime(startTime);
                endTime = validateAndFormatTime(endTime);
            } catch (error) {
                alert(error.message);
                return;
            }
            
            // 验证会议室是否存在
            if (window.meetingRooms) {
                const roomExists = window.meetingRooms.some(room => room.roomId === roomId);
                if (!roomExists) {
                    alert('指定的会议室不存在，请选择有效的会议室');
                    return;
                }
                
                // 如果会议室名称未填写或与编号不匹配，则自动填充
                const selectedRoom = window.meetingRooms.find(room => room.roomId === roomId);
                if (selectedRoom) {
                    document.getElementById('roomName').value = selectedRoom.roomName;
                }
            } else {
                // 如果会议室列表未加载完成，进行基本验证
                if (!document.getElementById('roomName').value.trim()) {
                    alert('请输入会议室名称');
                    return;
                }
            }
            
            // 验证时间格式
            const startDateTime = new Date(startTime);
            const endDateTime = new Date(endTime);
            
            if (endDateTime <= startDateTime) {
                alert('结束时间必须晚于开始时间');
                return;
            }
            
            // 构建会议信息对象
            const meeting = {
                roomId: roomId,
                roomName: document.getElementById('roomName').value,
                bookerName: bookerName,
                startTime: startTime,
                endTime: endTime,
                meetingTopic: meetingTopic
            };
            
            // 显示加载提示
            const bookingButton = document.getElementById('bookingButton');
            const originalButtonText = bookingButton.innerHTML;
            bookingButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 预订中...';
            bookingButton.disabled = true;
            
            // 发送预订请求到后端
            fetch('/api/meetings/bookings', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                    // 添加Accept头以确保正确的响应格式
                },
                body: JSON.stringify(meeting)
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    alert('会议室预订成功！\n会议室编号: ' + roomId + '\n会议室名称: ' + meeting.roomName);
                    // 清空表单
                    document.getElementById('bookingForm').reset();
                } else {
                    alert('预订失败: ' + data.msg);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('预订请求失败，请稍后重试');
            })
            .finally(() => {
                // 恢复按钮状态
                bookingButton.innerHTML = originalButtonText;
                bookingButton.disabled = false;
            });
        });
    </script>
</body>
</html>